// import com.alibaba.druid.DbType;
// import com.alibaba.druid.sql.ast.SQLName*;
// import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
// import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
// import com.alibaba.druid.sql.visitor.SQLASTVisitor;

#include "MysqlForeignKey.h"
#include "../../../ast/SQLName.h"
#include "../../../../DbType.h"
#include "../../../visitor/SQLASTVisitor.h"
#include "../visitor/MySqlASTVisitor.h"
#include "../../../../utils/instanceof.h"
#include "../../../../utils/listTran.h"

MysqlForeignKey::MysqlForeignKey()
{
  dbType = DbType_ptr(new DbType(&DbType::mysql));
}

SQLName_ptr MysqlForeignKey::getIndexName()
{
  return indexName;
}

void MysqlForeignKey::setIndexName(SQLName_ptr indexName)
{
  this->indexName = indexName;
}

BOOL_ptr MysqlForeignKey::isHasConstraint()
{
  return hasConstraint;
}

void MysqlForeignKey::setHasConstraint(BOOL_ptr hasConstraint)
{
  this->hasConstraint = hasConstraint;
}

// @Override
void MysqlForeignKey::accept0(SQLASTVisitor_ptr visitor)
{
  if (instanceof <SQLASTVisitor, MySqlASTVisitor>(visitor.get()))
  {
    // accept0(std::make_shared<MySqlASTVisitor>( dynamic_cast<MySqlASTVisitor *>(visitor.get())));
    MySqlASTVisitor_ptr tmp = std::dynamic_pointer_cast<MySqlASTVisitor>(visitor);
    accept0(tmp);
  }
}

void MysqlForeignKey::accept0(MySqlASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(MysqlForeignKey)))
  {
    acceptChild(visitor, this->getName());
    acceptChild(visitor, this->getReferencedTableName());
    SQLObject_list_ptr  tmp1 = ListTran::tran_class<SQLName, SQLObject>(this->getReferencingColumns());
    acceptChild(visitor, tmp1);
    SQLObject_list_ptr  tmp2 = ListTran::tran_class<SQLName, SQLObject>(this->getReferencedColumns());
    acceptChild(visitor, tmp2);

    acceptChild(visitor, indexName);
  }
  visitor->endVisit(SharedObject(MysqlForeignKey));
}

SQLObject_ptr MysqlForeignKey::clone()
{
  SQLObject_ptr clone_tmp = std::shared_ptr<MysqlForeignKey>(new MysqlForeignKey());
  std::shared_ptr<MysqlForeignKey> x = std::dynamic_pointer_cast<MysqlForeignKey>(clone_tmp);
  // MysqlForeignKey *x = new MysqlForeignKey();
  cloneTo(x);

  if (indexName != nullptr)
  {
    x->setIndexName(std::dynamic_pointer_cast<SQLName>(indexName->clone()));
  }

  x->referenceMatch = referenceMatch;
  x->onUpdate = onUpdate;
  x->onDelete = onDelete;

  return x;
}

SQLForeignKeyImpl_Match_ptr MysqlForeignKey::getReferenceMatch()
{
  return referenceMatch;
}

void MysqlForeignKey::setReferenceMatch(SQLForeignKeyImpl_Match_ptr referenceMatch)
{
  this->referenceMatch = referenceMatch;
}

SQLForeignKeyImpl_Option_ptr MysqlForeignKey::getOnUpdate()
{
  return onUpdate;
}

void MysqlForeignKey::setOnUpdate(SQLForeignKeyImpl_Option_ptr onUpdate)
{
  this->onUpdate = onUpdate;
}

SQLForeignKeyImpl_Option_ptr MysqlForeignKey::getOnDelete()
{
  return onDelete;
}

void MysqlForeignKey::setOnDelete(SQLForeignKeyImpl_Option_ptr onDelete)
{
  this->onDelete = onDelete;
}
